package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Main {
    //java 8   新特性  流和lamda表达式
    public static void main(String[] args) {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        new Random(10).ints().limit(1).forEach(System.out::println);
        filtered.forEach(System.out::println);
        testOptional();
        //这边的是对方法的引用
        final Main main = Main.create(Main::new);
        final List< Main > mains = Arrays.asList( main );
        mains.forEach( Main::repair );
    }

    //封装类型的容器，optional
    public static void testOptional(){
        //调用工厂方法创建Optional实例
        Optional<String> name = Optional.of("Sanaulla");
        //传入参数为null，抛出NullPointerException.
//        Optional<String> someNull = Optional.of(null);
        System.out.println(name);
//        System.out.println(someNull);
    }

    public void repair() {
        System.out.println( "Repaired " + this.toString() );
    }

    public static Main create( final Supplier< Main > supplier ) {
        return supplier.get();
    }
}
